package run;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.utils.ReferenceConfigCache;
import com.alibaba.dubbo.rpc.service.GenericService;

import test.TestInterface;
import test.TestInterface2;

public class ConsumerTest {
    public static void main(String[] args) {
//		List<Map<String, Object>> parameters = new ArrayList<Map<String,Object>>();
//		Map<String,Object> parameter1 = new HashMap<String,Object>();
//		parameter1.put("ParamType", "java.lang.String");
//		parameter1.put("Object", "123");
//		parameters.add(parameter1);
//		genericInvoke("test.testInterface","test1",parameters);

        TestInterface inter1 = (TestInterface) getService(TestInterface.class);
        String result = inter1.test1("abc");
        System.out.println(result);
        String result2 = inter1.test2("abc", "ggg");
        System.out.println(result2);

        TestInterface2 inter2 = (TestInterface2) getService(TestInterface2.class);
        long result3 = inter2.factorial(3);
        System.out.println(result3);
        long result4 = inter2.add(4, 5);
        System.out.println(result4);
    }

    public static Object getService(Class interfaceClass) {
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("consumer");
        ReferenceConfig<Object> reference = new ReferenceConfig<Object>();
        reference.setApplication(applicationConfig);
        reference.setRegistry(registry);
        reference.setInterface(interfaceClass); // 接口名
        reference.setVersion("1.0.0");
        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        Object genericService = cache.get(reference);
        return genericService;
    }

    public static Object genericInvoke(String interfaceClass, String methodName, List<Map<String, Object>> parameters) {

        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://127.0.0.1:2181");

        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("consumer");

        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        reference.setApplication(applicationConfig);
        reference.setRegistry(registry);
        reference.setInterface(interfaceClass); // 接口名 
        reference.setGeneric(true);             // 声明为泛化接口
        reference.setVersion("1.0.0");

        //ReferenceConfig实例很重，封装了与注册中心的连接以及与提供者的连接，
        //需要缓存，否则重复生成ReferenceConfig可能造成性能问题并且会有内存和连接泄漏。
        //API方式编程时，容易忽略此问题。
        //这里使用dubbo内置的简单缓存工具类进行缓存

        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);
        // 用com.alibaba.dubbo.rpc.service.GenericService可以替代所有接口引用 

        int len = parameters.size();
        String[] invokeParamTyeps = new String[len];
        Object[] invokeParams = new Object[len];
        for (int i = 0; i < len; i++) {
            invokeParamTyeps[i] = parameters.get(i).get("ParamType") + "";
            invokeParams[i] = parameters.get(i).get("Object");
        }
        return genericService.$invoke(methodName, invokeParamTyeps, invokeParams);
    }

}
